package com.easepal7506a.project.observer;

import com.easepal.socketiolib.model.SocketModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by FHB on 2018/4/18.
 */
public class SocketDataObserver {

    private static SocketDataObserver mInst = null;
    private static Object mLock = new Object();

    private List<SocketReceive> mSocketReceive;

    private SocketDataObserver(){
        mSocketReceive = new ArrayList<SocketReceive>();
    }

    public static SocketDataObserver getInst(){
        if(mInst == null){
            synchronized(mLock){
                if(mInst == null){
                    mInst = new SocketDataObserver();
                }
            }
        }
        return mInst;
    }

    public void  registerObserver(SocketReceive action){
        if(!mSocketReceive.contains(action)){
            mSocketReceive.add(action);
        }
    }

    public void removeObserver(SocketReceive action){
        if(mSocketReceive.contains(action)){
            mSocketReceive.remove(action);
        }

    }


    public void noticeSocket(SocketModel model){
        for (int i = 0; i < mSocketReceive.size(); i++) {
            try {
                mSocketReceive.get(i).onRceiveSocket(model);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public interface SocketReceive {
        //连接状态
        void onRceiveSocket(SocketModel model);


    }


}
